package com.mars.base.common.exception;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataBaseException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 插入记录失败
	 */
	public static final DataBaseException INSERT_ERROR = new DataBaseException("插入记录失败");
	/**
	 * 批量插入记录失败
	 */
	public static final DataBaseException INSERT_BATCH_ERROR = new DataBaseException("批量插入记录失败");
	/**
	 * 插入或修改记录失败
	 */
	public static final DataBaseException INSERT_UPDATE_ERROR = new DataBaseException("插入或修改记录失败");
	/**
	 * 修改记录失败
	 */
	public static final DataBaseException UPDATE_ERROR = new DataBaseException("修改记录失败");
	/**
	 * 批量修改记录失败
	 */
	public static final DataBaseException UPDATE_BATCH_ERROR = new DataBaseException("批量修改记录失败");
	/**
	 * 删除记录失败
	 */
	public static final DataBaseException DELETE_ERROR = new DataBaseException("删除记录失败");
	/**
	 * 查询记录失败
	 */
	public static final DataBaseException SELECT_ERROR = new DataBaseException("查询记录失败");
	/**
	 * 查询记录失败
	 */
	public static final DataBaseException NOT_FOUND_STATEMENT = new DataBaseException("找不到对应的Statement");

	protected String msg;

	public DataBaseException() {
	}

	public DataBaseException(String message, Object... args) {
		super(String.format(message, args));
		this.msg = String.format(message, args);
	}

	public DataBaseException(String message) {
		super(message);
		this.msg = message;
	}

	public DataBaseException(String message, Throwable cause) {
		super(message, cause);
	}

	public DataBaseException(Throwable cause) {
		super(cause);
	}

	public DataBaseException logInfo() {
		this.logger.info(this.msg);
		return new DataBaseException(this.msg);
	}

	public DataBaseException logError() {
		this.logger.error(this.msg);
		return new DataBaseException(this.msg);
	}

	public DataBaseException logError(Exception e) {
		return errorMsg(e);
	}

	public DataBaseException errorMsg(Exception e) {
		this.logger.error(this.msg + "\r\n" + ExceptionUtils.getMessage(e));
		return new DataBaseException(this.msg + " - " + ExceptionUtils.getRootCauseMessage(e)) ;
	}

}
